package com.ebelter.nb.utils;


import android.text.TextUtils;

import com.chipsea.healthscale.CsAlgoBuilderEx;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.nb.model.jg.jgbean.JGScaleR;

public class ScaleUtils {

    private static final String TAG = "ScaleUtils";
    private CsAlgoBuilderEx mBuilderEx = new CsAlgoBuilderEx();



    //服务器只返回部分数据 要计算补充完整
    public JGScaleR dealJGScaleR(JGScaleR jGScaleR) {

        float weight2 = (float) (NumUtils.string2Float(jGScaleR.deviceInfo.weight)*1.0/10.0);
        float fat2 = (float) (NumUtils.string2Float(jGScaleR.deviceInfo.fat)*1.0/10.0);
        float resistance2 = NumUtils.string2Float(jGScaleR.deviceInfo.resistor);
        int hart2 = (int) NumUtils.string2Float(jGScaleR.deviceInfo.hart);

        int sex = NbUtitls.getSex();
        int orSex = 1 - sex;

        int age = NbUtitls.getAge();
        if (age < 18) {
            age = 18;
        } else if (age > 80) {
            age = 80;
        }

        int height = NbUtitls.getHeight();
        if (height<=0){
            height=160;
        }

        int profession=NbUtitls.getProfession();



            LogUtils.i(TAG,"---收到的阻抗="+resistance2);
//            //实测阻抗
//            resistanceResult2= (float) ((resistanceResult*100+10000)/103-user.getHeight());
//            ULog.i(TAG,"实测阻抗="+resistanceResult2);

            //加密阻抗
            resistance2= (float) ((weight2*40+resistance2*60+10000)*1.0/100.0);
        LogUtils.i(TAG,"--加密阻抗="+resistance2);



        try {
            // 设置sdk基值
            mBuilderEx.setUserInfo(height, weight2
                    , (byte) orSex, age, resistance2);

            LogUtils.i(TAG, "用户  分析测量结果 要计算的信息----身高--" + height
                    + "---体重--" + weight2 + "----脂肪---" + fat2 + "----阻抗---" + resistance2 +
                    "----性别----=" + sex + "---取反后的性别--=" + orSex);

            //设置用户角色，1为运动员，0为普通用户
            mBuilderEx.setMode(profession);
        /*####################计算出各个指标值###############################*/
            float wr, bmr, vf, mv, sm, bv, protein, bmi;
            //BMI
            bmi = getBmi(weight2, height);

            int ba, score;
            if (fat2 > 0.0f && weight2 > 0 && (resistance2 > 0 && resistance2 < 65535)) {//脂肪率大于0则，计算各个指标
                wr = mBuilderEx.getTFR();
                bmr = mBuilderEx.getBMR();
                vf = mBuilderEx.getVFR();
                //骨骼肌
                mv = mBuilderEx.getSMM();
                sm = mBuilderEx.getSLM();
                bv = mBuilderEx.getMSW();
                ba = (int) mBuilderEx.getBodyAge();
                protein = mBuilderEx.getPM();
                score = mBuilderEx.getScore();
                // 蛋白质KG  转 百分比
                protein = dbzKgToPer(protein, weight2);
                LogUtils.i(TAG, "---- wr =" + wr + ", bmr =" + bmr + ", vf =" + vf + ", mv =" + mv + ", bv =" + bv+",sm ="+sm+", bmi ="+bmi
                        + ", fatResult = " + fat2 + ", weightResult = " + weight2 + ", ba = " + ba + ", protein = " + protein + ", score = " + score);



            } else {//脂肪率小于等于0，则各个指标无效
                wr = bmr = vf = mv = sm = bv = protein = 0f;
                ba = score = 0;
            }


            jGScaleR.deviceInfo.fat2 = fat2;
            jGScaleR.deviceInfo.weight2 = weight2;
            jGScaleR.deviceInfo.resistor2 = (int) resistance2;

            jGScaleR.deviceInfo.water = wr;
            jGScaleR.deviceInfo.bmr = bmr;
            jGScaleR.deviceInfo.bmi = bmi;
            jGScaleR.deviceInfo.visceralFat = vf;
            jGScaleR.deviceInfo.muscleV = sm;//这里的sdk：肌肉量和骨骼肌反着的
            jGScaleR.deviceInfo.skeletalMuscleV = mv;
            jGScaleR.deviceInfo.boneV = bv;


            int age2 = NbUtitls.getAge();
            if (ba>age2+10){
                ba=age2+10;
            }else if (ba<age2-10){
                ba=age2-10;
            }

            if (ba<0){
                ba=0;
            }

            jGScaleR.deviceInfo.bodyAge = ba;
            jGScaleR.deviceInfo.protein = protein;
            jGScaleR.deviceInfo.score = score;
            jGScaleR.deviceInfo.hart2 = hart2;

        } catch (Exception e) {
            LogUtils.e(TAG, "-------getResult--------e.getMessage()= " + e.getMessage());
        }
        return jGScaleR;
    }

    private float dbzKgToPer(float dbz, float weight) {
        float preF = dbz / weight * 100;
        preF = Math.round(preF * 10) / 10.0f;
        preF = Math.min(preF, 100.0f);
        return preF;
    }

    private float getBmi(float weight, float userHeight) {
        float bmi = weight / (userHeight * userHeight / 10000);
        return Math.round(bmi * 10) / 10.0f;
    }
}
